package com.homwee.serialportinspection.serialport;

import java.util.ArrayList;
import java.util.List;

public abstract class SerialPort {
    protected List<Listener> mListeners = new ArrayList<>();

    public static interface Listener {
        void onData(byte[] data);

        void onString(String string);
    }

    public abstract void init();

    public abstract void uninit();

    public abstract boolean start();

    public abstract boolean stop();

    public abstract byte[] read(int size);

    public abstract int write(byte[] data);

    public abstract String readString();

    public abstract int writeString(String string);

    public boolean registerListener(Listener listener) {
        if (mListeners.contains(listener)) {
            return false;
        } else {
            mListeners.add(listener);

            return true;
        }
    }

    public boolean unregisterListener(Listener listener) {
        if (mListeners.contains(listener)) {
            mListeners.remove(listener);
            return true;
        } else {
            return true;
        }
    }
}
